package com.xjj.util.handler;

import com.xjj.entity.ResponseEntity;
import com.xjj.util.exception.RepeatedSmsException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author : 王泰元
 * @date : 2022/10/20 16:31
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<String> exceptionHandler(Exception e) {
        log.error("error >>>>>>>>>>>> ", e);
        if (e.getMessage().length() > 20) {
            return ResponseEntity.FAIL("服务器异常", null);
        }
        return ResponseEntity.FAIL(e.getMessage(), null);
    }


    /**
     * 处理重复获取验证码异常
     *
     * @param e 异常
     * @return ResponseEntity
     */
    @ExceptionHandler(value = RepeatedSmsException.class)
    public ResponseEntity<String> repeatedSmsExceptionHandler(RepeatedSmsException e) {
        log.error("error >>>>>>>>>>>> ", e);

        return new ResponseEntity<>(301, e.getMessage(), null);
    }
}
